package com.huluobo.a2208mdmdemo2.myview

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.Log
import android.view.MotionEvent
import android.view.animation.CycleInterpolator
import android.widget.ImageView
import android.widget.RelativeLayout
import com.huluobo.a2208mdmdemo2.R
import java.util.concurrent.Flow
import kotlin.math.roundToInt

/**
 *  Created by LC on 2025/2/13.
 */
class LikeView(context: Context?) : RelativeLayout(context) {

    fun start(x: Float, y: Float) {
        val iv = ImageView(context)
        iv.setImageResource(R.drawable.ic_action_zan)

        val params = LayoutParams(200, 200)
        params.leftMargin = x.roundToInt() - 50
        params.topMargin = y.roundToInt() - 200
        iv.layoutParams = params

        val animator = ObjectAnimator.ofFloat(iv, "alpha", 1f, 0f)
//        val animatorRotation = ObjectAnimator.ofFloat(iv, "rotation", x, 100f, x)
//        animatorRotation.interpolator = CycleInterpolator(5f)
        val scaleX = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 2f)
        val scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 2f)

        val set = AnimatorSet()
        set.playTogether(animator, scaleX, scaleY)
        set.duration = 1000
        set.start()
        addView(iv)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        val x = event?.x
        val y = event?.y
        if (event?.action == MotionEvent.ACTION_DOWN) {
            Log.i("TagA", "event:$x,$y")
            start(x!!, y!!)
        }
        return true
    }
}